package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.vo.SpecVo;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("specificationController")
public class SpecificationController {
    //开启日志
    Logger logger = LoggerFactory.getLogger(BrandController.class);

    @Reference(check=false ,timeout=106666)
    private SpecificationService specificationService;

    //跳转到规格管理页面
    @RequestMapping("specificationList")
    public String specificationList(){
        return "admin/specification";
    }

    //查询规格表信息
    @RequestMapping("getSpecification")
    @ResponseBody
    public Result getSpecification(String specName,@RequestParam(defaultValue = "1") Integer pageNumber, @RequestParam(defaultValue = "3") Integer pageSize){
        try {
            logger.info("规格查询方法开始运行{}");
            PageBean<TbSpecification> specification = specificationService.getSpecification(specName,pageNumber, pageSize);
            return Result.response(ResultEnum.SUCCESS).setData(specification);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //规格增加操作
    @RequestMapping("insertSpec")
    @ResponseBody
    public Result insertSpec(String specName,Integer[] orders,String[] optionName){
        try {
            logger.info("规格增加方法开始运行{}");
            specificationService.insertSpec(specName,orders,optionName);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //规格删除操作
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]")Integer[] ids){
        try {
            logger.info("规格删除方法开始运行{}");
            specificationService.deleteBatch(ids);
        }catch (Exception e){
            Result.response(ResultEnum.ERROR);
            logger.error("出现错误，查看日志{}",e);
        }
        return Result.response(ResultEnum.SUCCESS);
    }

    //规格表元素和规格选项表元素联查，回显用
    @RequestMapping("getSpecVoById")
    @ResponseBody
    public Result getSpecVoById(Integer id){
        try {
            logger.info("规格回显方法开始运行{}");
            List<SpecVo> list = specificationService.getSpecVoById(id);
            return Result.response(ResultEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }
    //规格修改
    @RequestMapping("updateSpec")
    @ResponseBody
    public Result updateSpec(TbSpecification specification,Integer[] orders,String[] optionName){
        try {
            logger.info("规格修改方法开始运行{}");
            specificationService.updateSpec(specification,orders,optionName);
            return Result.response(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.error("出现错误，查看日志{}",e);
            return Result.response(ResultEnum.ERROR);
        }
    }
}
